// pages/workOrder/list/index.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'

var pageSize = 20
var height = 0

wx.getSystemInfo({
  success: function (res) {
    height = res.windowHeight - 50
  },
})

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '-2',
    scrollH: height,
    orderList: {
      datas: [],
      currentPage: 1,
      totalPage: 0,
      pageSize: pageSize
    },
    completedOrderList: {
      datas: [],
      currentPage: 1,
      totalPage: 0,
      pageSize: pageSize
    }


  },

  /**切换Tab标签 */
  handleTabChange({ detail = {}}) {
    console.log('handleTabChange', detail)
    this.setData({
      currentTab: detail.key
    })
    if (detail.key === -1) {
      if (this.data.completedOrderList.datas.length == 0) {
        this.getMoreOrderList(-1, 1)
      }
    } else {
      if (this.data.orderList.datas.length == 0) {
        this.getMoreOrderList(-2, 1)
      }
    }
  },

  /** */
  handleLoadOrderList(e) {
    console.log('handleLoadOrderList', e)
    let key = e.currentTarget.id
    let target = ''
    if (key == -1) {
      target = 'completedOrderList'
    } else {
      target = 'orderList'
    }
    if(this.data[target].currentPage<this.data[target].totalPage){
      this.getMoreOrderList(key, this.data[target].currentPage+1)
    }
  },

  /**加载更多的工单列表 */
  getMoreOrderList(type, page) {
    let url = urls.workOrder.getOrderList + '?currentpage=' + page + '&&pagesize=' + pageSize
    let param = {
      "openid": this.data.openid,
      "status": type
    }
    requestUtil.postRequest(url, param, (res) => {
      console.log(res)
      let target = 'orderList'
      if (type === -1) {
        target = 'completedOrderList'
      }
      let totalPage = res.total % pageSize == 0 ? res.total / pageSize : res.total / pageSize + 1
      this.setData({
        [`${target}.datas`]: this.data[target].datas.concat(res.list) ,
        [`${target}.totalPage`]: totalPage,
        [`${target}.currentPage`]: page
      })
    }, () => { })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let openid = wx.getStorageSync('openid')
    this.setData({
      openid
    })

    this.getMoreOrderList(-2, 1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})